package com.imoocdl.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.imoocdl.dataobject.OrderDetail;
import com.imoocdl.dto.OrderDtDTO;
import com.imoocdl.enums.ResultEnum;
import com.imoocdl.exception.SellException;
import com.imoocdl.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 装换类
 * Created by pc on 2018/7/18.
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDtDTO converter(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDtDTO orderDtDTO = new OrderDtDTO();
        orderDtDTO.setBuyerName(orderForm.getName());
        orderDtDTO.setBuyerPhone(orderForm.getPhone());
        orderDtDTO.setBuyerOpenid(orderForm.getOpenid());
        orderDtDTO.setBuyerAddress(orderForm.getAddress());
        List<OrderDetail> orderDetailList = new ArrayList<>();
        try {
            orderDetailList =  gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("[对象转换错误] string={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDtDTO.setOrderDetailList(orderDetailList);
        return orderDtDTO;
    }
}
